<?php
require_once 'ConnectDB.php';
require_once 'SanPhamDTO.php';
class SanPhamDAO extends ConnectDB {
	/**
	 * 
	 * Enter description here ...
	 */
	function SanPhamDAO(){
		
	}
	/**
	 * Thêm sản phẩm
	 * Enter description here ...
	 * Cập nhật by Quang 26/6/2011
	 */
	public static function ThemSanPham($sanPhamDto)
	{
		$result = true;
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "insert into san_pham(`Ma`, `MaSanPham`, `TenSanPham`, `SoLuong`, `DonGiaGoc`, `DonGiaBan`, `DacDiemSP`, `NgayDang`, `NgayCapNhat`, `NguoiCapNhat`, `DaXoa`, `NguoiXoa`, `NgayXoa`, `HinhAnh`, `LuotXem`, `MaLoaiSP`)
					values ($sanPhamDto->Ma,
							'$sanPhamDto->MaSanPham',
							N'$sanPhamDto->TenSanPham',
							$sanPhamDto->SoLuong,
							$sanPhamDto->DonGiaGoc,
							$sanPhamDto->DonGiaBan,
							N'$sanPhamDto->DacDiemSP',
							now(),
							'$sanPhamDto->NgayCapNhat',
							'$sanPhamDto->NguoiCapNhat',
							$sanPhamDto->DaXoa,
							'$sanPhamDto->NguoiXoa',
							'$sanPhamDto->NgayXoa',
							'$sanPhamDto->HinhAnh',
							$sanPhamDto->LuotXem,
							$sanPhamDto->MaLoaiSP
							);";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Cập nhật thông tin sản phẩm
	 * edit Thu Hà 24/6/2011...
	 */
	public static function CapNhatThongTinSanPham($sanPhamDto)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE san_pham as sp 
			SET sp.TenSanPham = $sanPhamDto->TenSanPham, sp.SoLuong = $sanPhamDto->SoLuong,
				sp.DonGiaGoc = $sanPhamDto->DonGiaGoc, sp.DonGiaBan = $sanPhamDto->DonGiaBan, 
				sp.DacDiemSP = $sanPhamDto->DacDiemSP, sp.NgayCapNhat = $sanPhamDto->NgayCapNhat, 
				sp.NguoiCapNhat = $sanPhamDto->NguoiCapNhat, sp.HinhAnh = $sanPhamDto->HinhAnh
			WHERE sp.Ma = $sanPhamDto->Ma;";
			echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Cập nhật lượt xem của sản phẩm ( sp chưa bị xóa mới được cập nhật)
	 * Edit by Thu Hà 20/6/2011.
	 */
	public static function CapNhatLuotXemCuaSanPham($maSP, $maNguoiDung)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE san_pham as sp 
				SET sp.LuotXem = sp.LuotXem + 1, sp.NgayCapNhat= now(), sp.NguoiCapNhat = $maNguoiDung
				WHERE sp.Ma = $maSP AND sp.DaXoa = 0;";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
		
	/**
	 * Lấy danh sách sản phẩm theo thứ tự view, ngày đăng giảm dần
	 * $soLuong = TOP n
	 * edit 27/6/2011 - thêm dk loại sp chưa bị xóa
	 */
	public  static  function LayDanhSachSanPham($soLuong)
	{
		$lstSanPham = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			if ($soLuong > 0)
				$strSQL = "SELECT * 
							FROM `San_pham`
							WHERE DaXoa = 0
								  AND MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)
							ORDER BY `LuotXem` DESC, `NgayDang` DESC 
							LIMIT 0, $soLuong;";
			else
				$strSQL = "SELECT * 
							FROM `San_pham` 
							WHERE DaXoa = 0 
								 AND MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)
							ORDER BY `LuotXem` DESC, `NgayDang` DESC;";
			
			//echo $strSQL;
				
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
				$lstSanPham = array();
			while ($row = mysql_fetch_array($result) )
			{
				
				$sanPhamDto = new SanPhamDTO();
				$sanPhamDto->Ma = $row["Ma"];
				$sanPhamDto->TenSanPham = $row["TenSanPham"];
				$sanPhamDto->SoLuong = $row["SoLuong"];
				$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
				$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
				$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
				$sanPhamDto->NgayDang = $row["NgayDang"];
				$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
				$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$sanPhamDto->DaXoa = $row["DaXoa"];
				$sanPhamDto->NgayXoa = $row["NgayXoa"];
				$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
				$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
				$sanPhamDto->HinhAnh =$row["HinhAnh"];
				$sanPhamDto->LuotXem = $row["LuotXem"];
				
				array_push($lstSanPham, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = array();
		}
		return $lstSanPham;
	}
	
	/**
	 * Lấy sản phẩm theo mã sản phẩm
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Thu Hà 20/6/2011	 
	 */
	public  static  function LaySanPhamTheoMa($maSanPham, $tinhTrang)
	{
		$sanPhamDto = new SanPhamDTO();
		try {
			if (!ConnectDB::OpenConnection())
				return null;
		
			$strSQL = "select * from 
						San_Pham sp 
						where sp.Ma = $maSanPham
							  AND MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)";
			if ($tinhTrang != 2)
				$strSQL .= " AND sp.DaXoa = $tinhTrang ";
					
			//echo $strSQL;
				
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result || mysql_num_rows($result) <= 0)
			{
				return null;
			}
			$row = mysql_fetch_array($result);				
				
			$sanPhamDto->Ma = $row["Ma"];
			$sanPhamDto->MaSanPham = $row["MaSanPham"];
			$sanPhamDto->TenSanPham = $row["TenSanPham"];
			$sanPhamDto->SoLuong = $row["SoLuong"];
			$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
			$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
			$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
			$sanPhamDto->NgayDang = $row["NgayDang"];
			$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
			$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
			$sanPhamDto->DaXoa = $row["DaXoa"];
			$sanPhamDto->NgayXoa = $row["NgayXoa"];
			$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
			$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
			$sanPhamDto->HinhAnh =$row["HinhAnh"];
			$sanPhamDto->LuotXem = $row["LuotXem"];
				
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$sanPhamDto = null;
		}
		return $sanPhamDto;
	}
	
	
	/**
	 * Lấy danh sách sản phẩm theo gian hàng
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Thu Hà 20/6/2011
	 */
	public  static  function LayDanhSachSanPhamTheoGianHang($maGianHang, $tinhTrang)
	{
		$lstSanPham = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT sp.Ma, sp.TenSanPham, sp.SoLuong, sp.DonGiaGoc, sp.DonGiaBan, sp.DacDiemSP, sp.NgayDang, sp.NgayCapNhat, sp.NguoiCapNhat, sp.DaXoa, sp.NgayXoa, sp.NguoiXoa, sp.MaLoaiSP, sp.HinhAnh, sp.LuotXem
							FROM san_pham sp, loai_san_pham lsp
							WHERE sp.MaLoaiSP = lsp.MaLoaiSP 
								AND lsp.MaGianHang = $maGianHang 
								AND lsp.TrangThai = 0";
				if ($tinhTrang != 2)
					$strSQL .= " AND sp.DaXoa = $tinhTrang ";
							
				$result = mysql_query($strSQL, ConnectDB::$mLink);
				if(!$result)
					return null;
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];					
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	
	/**
	 * Lấy danh sách sản phẩm theo loại sản phẩm
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Thu Hà 20/6/2011
	 */
	public  static  function LayDanhSachSanPhamTheoLoaiSanPham($maLoaiSP, $tinhTrang)
	{
		$lstSanPham = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM san_pham as sp 
						WHERE sp.MaLoaiSP = $maLoaiSP 
						AND sp.MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)";
			if ($tinhTrang != 2)
				$strSQL .= " AND sp.DaXoa = $tinhTrang";
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$sanPhamDto = new SanPhamDTO();
				$sanPhamDto->Ma = $row["Ma"];
				$sanPhamDto->MaSanPham = $row["MaSanPham"];
				$sanPhamDto->TenSanPham = $row["TenSanPham"];
				$sanPhamDto->SoLuong = $row["SoLuong"];
				$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
				$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
				$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
				$sanPhamDto->NgayDang = $row["NgayDang"];
				$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
				$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$sanPhamDto->DaXoa = $row["DaXoa"];
				$sanPhamDto->NgayXoa = $row["NgayXoa"];
				$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
				$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
				$sanPhamDto->HinhAnh =$row["HinhAnh"];
				$sanPhamDto->LuotXem = $row["LuotXem"];				
				
				array_push($lstSanPham, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}

	/**
	 * Xóa sản phẩm
	 */
	public static function XoaSanPham($maSanPham, $maNguoiDung)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE san_pham as sp 
						SET sp.DaXoa = 1, sp.NgayXoa = now(), sp.NguoiXoa = $maNguoiDung
						WHERE sp.Ma = $maSanPham;";
						
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Lấy danh sách sản phẩm mới nhất theo username (sản phẩm mới nhất là sản phẩm có (ngày hiện tại - ngày đăng) < 3 )
	 * Có ý nghĩa gì ???
	 */
	public static function LayDSSanPhamMoi($username)
	{
		$lstSanPham = array();
		$ngayhientai = date("Y-m-d");
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = ("SELECT sp.Ma, sp.TenSanPham, sp.DonGiaBan, sp.NgayDang, sp.HinhAnh, sp.LuotXem, sp.SoLuong, sp.DacDiemSP
						FROM nguoi_dung nd, gian_hang gh, loai_san_pham lsp, san_pham sp
						WHERE nd.UserName = '$username' and nd.MaGianHang = gh.MaGianHang 
								and lsp.MaGianHang = gh.MaGianHang and sp.MaLoaiSP = lsp.MaLoaiSP
								and (($ngayhientai - sp.NgayDang) < 3) and sp.DaXoa = 0
								and lsp.TrangThai = 0
						");// daxoa = 0 => chưa xoa, daxoa = 1 => xóa rùi
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
				$lstSanPham = array();
			while ($row = mysql_fetch_array($result) )
			{
				$sanPhamDto = new SanPhamDTO();
				$sanPhamDto->Ma = $row["Ma"];
				$sanPhamDto->TenSanPham = $row["TenSanPham"];
				$sanPhamDto->SoLuong = $row["SoLuong"];
				$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
				$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
				$sanPhamDto->NgayDang = $row["NgayDang"];
				$sanPhamDto->HinhAnh =$row["HinhAnh"];
				$sanPhamDto->LuotXem = $row["LuotXem"];
				
				
				array_push($lstSanPham, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = array();
		}
		return $lstSanPham;

	}
	/**
	 * Ngọc Hà 12/6/2011
	 * Lấy danh mã gian hàng của sản phẩm
	 * Enter description here ...
	 */
	public  static  function LayMaGianHangCuaSanPham($maSanPham)
	{
		$res = 0;
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT lsp.MaGianHang as MaGianHang
							FROM san_pham sp, loai_san_pham lsp
							WHERE sp.Ma = $maSanPham 
								AND sp.MaLoaiSP = lsp.MaLoaiSP 
								and sp.DaXoa = 0
								AND lsp.TrangThai = 0;";

				$result = mysql_query($strSQL, ConnectDB::$mLink);
				
				$row = mysql_fetch_array($result);
				$res = $row["MaGianHang"];
				if(!$result)
					$res = 0;
				
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$res = 0;
		}
		return $res;
	}
	
	/**
	 * Cập nhật giá sản phẩm //khi tạo sự kiện mới// khi load sản phẩm
	 * Enter description here ...
	 */
	public static function CapNhatGiaSanPham($maSP, $giaMoi)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE `san_pham`  
			SET `DonGiaBan` = $giaMoi
			WHERE `Ma` = $maSP";
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	/**
	 * Lấy danh sách sản phẩm theo mã sự kiện
	 * Edit by Thu Hà 20/6/2011
	 */
	public  static  function LayDanhSachSanPhamTheoMaSuKien($maSuKien)
	{
		$lstSanPham = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT sp.Ma, sp.TenSanPham, sp.SoLuong, sp.DonGiaGoc, sp.DonGiaBan, sp.DacDiemSP, sp.NgayDang, sp.NgayCapNhat, sp.NguoiCapNhat, sp.DaXoa, sp.NgayXoa, sp.NguoiXoa, sp.MaLoaiSP, sp.HinhAnh, sp.LuotXem
							FROM san_pham sp, chi_tiet_su_kien ctsk
							WHERE sp.Ma = ctsk.Ma 
								AND ctsk.MaSuKien = $maSuKien 
								AND sp.DaXoa = 0
								AND sp.MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)";
				
				
				$result = mysql_query($strSQL, ConnectDB::$mLink);
				if(!$result)
					return null;
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];					
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	
	/**
	 * Tìm kiếm sản phẩm
	 * Edit by Thu Hà 25/6/2011
	 */
	public  static  function TimKiemSanPham($tenSP, $loaiSP, $tenGH, $giaMin, $giaMax, $ngayDangTu, $ngayDangDen)
	{
		$lstSanPham = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT sp.Ma, sp.TenSanPham, sp.SoLuong, sp.DonGiaGoc, 
									sp.DonGiaBan, sp.DacDiemSP, sp.NgayDang, sp.NgayCapNhat, 
									sp.NguoiCapNhat, sp.DaXoa, sp.NgayXoa, sp.NguoiXoa, 
									sp.MaLoaiSP, sp.HinhAnh, sp.LuotXem
							FROM san_pham sp, loai_san_pham lsp, gian_hang gh
							WHERE sp.DaXoa = 0 AND lsp.TrangThai = 0
								AND sp.MaLoaiSP = lsp.MaLoaiSP 
								AND lsp.MaGianHang = gh.MaGianhang ";
				if (strlen($tenSP) >0)
				{
					$strSQL .= " AND sp.TenSanPham like '%$tenSP%'";
				}
				if (strlen($loaiSP) >0)
				{
					$strSQL .= " AND lsp.TenLoaiSP like '%$loaiSP%'";
				}
				if (strlen($tenGH) >0)
				{
					$strSQL .= " AND gh.TenGianHang like '%$tenGH%'";
				}
				if ($giaMin > 0)
				{
					$strSQL .= " AND sp.DonGiaBan >= $giaMin";
				}
				if ($giaMax > 0)
				{
					$strSQL .= " AND sp.DonGiaBan <= $giaMax";
				}
				if (strlen($ngayDangTu) > 0)
				{
					$strSQL .= " AND sp.NgayDang >= '$ngayDangTu'";
				}
				if (strlen($ngayDangDen) > 0)
				{
					$strSQL .= " AND sp.NgayDang <= '$ngayDangDen'";
				}
				
				$result = mysql_query($strSQL, ConnectDB::$mLink);
				if(!$result || mysql_num_rows($result) <= 0)
					return null;
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];					
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	/**
	 * Lấy danh sách sản phẩm mới của gian hàng
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Ngọc Hà 21/6/2011	 * 
	 * 25/6/2011: bỏ thuộc tính đánh giá - Thu Hà
	 */
	 
	 public static function LayDSSanPhamMoiTheoMaGH($maGianHang)
	{
		$lstSanPham = array();
		$ngayhientai = date("Y-m-d");
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT sp.Ma, sp.TenSanPham, sp.DonGiaBan, sp.NgayDang, sp.HinhAnh, sp.LuotXem, sp.SoLuong, sp.DacDiemSP
						FROM  gian_hang gh, loai_san_pham lsp, san_pham sp
						WHERE gh.MaGianHang = $maGianHang 
						and gh.MaGianHang = lsp.MaGianHang 
						and sp.MaLoaiSP = lsp.MaLoaiSP
						and lsp.MaGianHang = gh.MaGianHang 
						and sp.MaLoaiSP = lsp.MaLoaiSP
						and (('$ngayhientai' - sp.NgayDang) <= 3) 
						and sp.DaXoa = 0 
						and gh.DaXoa = 0
						and lsp.TrangThai = 0;
						";// daxoa = 0 => chưa xoa, daxoa = 1 => xóa rùi
			
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result || mysql_num_rows($result) <= 0)
				$lstSanPham = array();
			else
			{
				while ($row = mysql_fetch_array($result) )
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = array();
		}
		return $lstSanPham;

	}
	
	/**
	 * Lấy danh sách sản phẩm có view cao của gian hàng
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Ngọc Hà 21/6/2011
	 * 25/6/2011: bỏ thuộc tính đánh giá - Thu Hà
	 */
	public  static  function LayDanhSachSanPhamViewCaoCuaGH($maGianHang)
	{
		$lstSanPham = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM `San_pham` sp, loai_san_pham lsp
						WHERE sp.DaXoa = 0 
							and sp.MaLoaiSP = lsp.MaLoaiSP 
							and lsp.MaGianHang = $maGianHang
							and lsp.TrangThai = 0
						ORDER BY sp.LuotXem DESC limit 0,5;";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$sanPhamDto = new SanPhamDTO();
				$sanPhamDto->Ma = $row["Ma"];
				$sanPhamDto->TenSanPham = $row["TenSanPham"];
				$sanPhamDto->SoLuong = $row["SoLuong"];
				$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
				$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
				$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
				$sanPhamDto->NgayDang = $row["NgayDang"];
				$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
				$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$sanPhamDto->DaXoa = $row["DaXoa"];
				$sanPhamDto->NgayXoa = $row["NgayXoa"];
				$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
				$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
				$sanPhamDto->HinhAnh =$row["HinhAnh"];
				$sanPhamDto->LuotXem = $row["LuotXem"];		
				
				array_push($lstSanPham, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	
	
	/**
	 * Tính số lượng tồn của sản phẩm
	 * Edit by Ngọc Hà 23/6/2011
	 * edit 25/6/2011 - Thu Hà: sửa hàm count --> sum 
	 */
	public  static  function TinhSoLuongSanPhamDaMua($maSP)
	{
		$sum = 0;
		try {
			
			if (!ConnectDB::OpenConnection())
			{
				return false;
			}
			
			$strSql ="SELECT sum(SoLuong) as SoLuongDaMua FROM `chi_tiet_dat_hang` WHERE `MaSanPham` = $maSP;";
			$result = mysql_query($strSql, ConnectDB::$mLink);
			
			if (!$result || mysql_num_rows($result) != 1)
				return 0;
				
			$row = mysql_fetch_array($result);
			$sum = $row["SoLuongDaMua"];
			
			ConnectDB::CloseConnection();
			
		} 
		catch (Exception $e) {
			$sum = 0;
		}
		return $sum;
	}
	
	/**
	 * Tìm kiếm sản phẩm 
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Ngọc Hà 22/6/2011
	 * edit 25/6/2011 Thu Hà
	 */
	public  static  function TimKiemSanPham_GianHang($maGianHang, $tenSP, $loaiSP, $giaTu, $giaDen, $tuNgay, $denNgay, $giamTu, $giamDen)
	{
		$lstSanPham = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
			$strSQL = "SELECT * FROM San_pham sp, loai_san_pham lsp
						WHERE sp.DaXoa = 0 
						and sp.MaLoaiSP = lsp.MaLoaiSP 
						and lsp.MaGianHang = $maGianHang
						and lsp.TrangThai = 0
						and sp.TenSanPham like '%$tenSP%'";
			if($loaiSP > 0)
				$strSQL .= " and lsp.MaLoaiSP = $loaiSP";
			if(strlen($giaTu) > 0 && is_numeric($giaTu))
				$strSQL .= " and sp.DonGiaBan >= $giaTu";
			if(strlen($giaDen ) > 0 && is_numeric($giaDen ))
				$strSQL .= " and sp.DonGiaBan <= $giaDen";
			if(strlen($tuNgay ) > 0)
				$strSQL .= " and sp.NgayDang >= '$tuNgay'";
			if(strlen($denNgay ) > 0)
				$strSQL .= " and sp.NgayDang <= '$denNgay'";
			if(strlen($giamTu) > 0 && is_numeric($giamTu))
				$strSQL .= " and sp.Ma in (select ct.Ma from su_kien sk, chi_tiet_su_kien ct 
											where sk.MaGianHang = $maGianHang 
											and sk.MaSuKien = ct.MaSuKien 
											and sk.PhanTramGiamGia >= $giamTu )";
			if(strlen($giamDen) > 0 && is_numeric($giamDen))
				$strSQL .= " and sp.Ma in (select ct.Ma 
											from su_kien sk, chi_tiet_su_kien ct 
											where sk.MaGianHang = $maGianHang 
											and sk.MaSuKien = ct.MaSuKien 
											and sk.PhanTramGiamGia <= $giamDen)";
			else
				$strSQL .= ";";
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$sanPhamDto = new SanPhamDTO();
				$sanPhamDto->Ma = $row["Ma"];
				$sanPhamDto->TenSanPham = $row["TenSanPham"];
				$sanPhamDto->SoLuong = $row["SoLuong"];
				$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
				$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
				$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
				$sanPhamDto->NgayDang = $row["NgayDang"];
				$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
				$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$sanPhamDto->DaXoa = $row["DaXoa"];
				$sanPhamDto->NgayXoa = $row["NgayXoa"];
				$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
				$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
				$sanPhamDto->HinhAnh =$row["HinhAnh"];
				$sanPhamDto->LuotXem = $row["LuotXem"];			
				
				array_push($lstSanPham, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	/**
	 * Anh Vũ 25/06
	 * Lấy danh sách sản phẩm mới up
	 * 0 : lấy tất cả 
	 */
	public  static  function LayDanhSachSanPhamMoiUP($number)
	{
		$lstSanPham = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT *
							FROM san_pham sp 
							WHERE sp.DaXoa = 0
								  AND MaLoaiSP IN (SELECT MaLoaiSP FROM loai_san_pham WHERE TrangThai = 0)
							ORDER BY `LuotXem` DESC, `NgayDang` ASC";
				if ($number > 0)
					$strSQL .= " LIMIT 0, $number ";
				$strSQL .= " ;";
				$result = mysql_query($strSQL);
				if(!$result)
					$lstSanPham = array();
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];
					
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = array();
		}
		return $lstSanPham;
	}
	
	
	
	/**
	 * Lấy danh sách sản phẩm theo gian hàng
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Thu Hà 20/6/2011
	 */
	public  static  function LayDanhSachSanPhamTheoGianHang_PhanTrang($maGianHang, $tinhTrang, $start, $display)
	{
		$lstSanPham = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT sp.Ma, sp.MaSanPham, sp.TenSanPham, sp.SoLuong, sp.DonGiaGoc, sp.DonGiaBan, sp.DacDiemSP, sp.NgayDang, sp.NgayCapNhat, sp.NguoiCapNhat, sp.DaXoa, sp.NgayXoa, sp.NguoiXoa, sp.MaLoaiSP, sp.HinhAnh, sp.LuotXem
							FROM san_pham sp, loai_san_pham lsp
							WHERE sp.MaLoaiSP = lsp.MaLoaiSP 
								AND lsp.MaGianHang = $maGianHang
								AND lsp.TrangThai = 0
							";
				if ($tinhTrang != 2)
					$strSQL .= " AND sp.DaXoa = $tinhTrang LIMIT $start, $display ";
				$result = mysql_query($strSQL, ConnectDB::$mLink);
				if(!$result)
					return null;
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->MaSanPham = $row["MaSanPham"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];					
					
					array_push($lstSanPham, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSanPham = null;
		}
		return $lstSanPham;
	}
	
		
	public  static  function TimKiemSanPham_All($keyword, $tinhTrang, $start, $display)
	{
		$lstKetQua = array();
		ConnectDB::OpenConnection();
		try {
			if (!ConnectDB::OpenConnection())
				return;
			else
			{
				$strSQL = "	SELECT sp.Ma, sp.MaSanPham, sp.TenSanPham, sp.SoLuong, sp.DonGiaGoc, 
									sp.DonGiaBan, sp.DacDiemSP, sp.NgayDang, sp.NgayCapNhat, 
									sp.NguoiCapNhat, sp.DaXoa, sp.NgayXoa, sp.NguoiXoa, 
									sp.MaLoaiSP, sp.HinhAnh, sp.LuotXem
							FROM san_pham sp, loai_san_pham lsp, gian_hang gh
							WHERE sp.DaXoa = 0 
								AND sp.MaLoaiSP = lsp.MaLoaiSP 
								AND lsp.MaGianHang = gh.MaGianhang
								AND lsp.TrangThai = 0
								AND (sp.TenSanPham like '%$keyword%'
								OR lsp.TenLoaiSP like '%$keyword%'
								OR gh.TenGianHang like '%$keyword%'
								OR sp.DonGiaBan like '%$keyword%'
								OR sp.DonGiaBan like '%$keyword%'
								OR sp.NgayDang like '%$keyword%'
								OR sp.NgayDang like '%$keyword%')
							LIMIT $start, $display";
							
				$result = mysql_query($strSQL, ConnectDB::$mLink);
				if(!$result || mysql_num_rows($result) <= 0)
					return null;
				while ($row = mysql_fetch_array($result))
				{
					$sanPhamDto = new SanPhamDTO();
					$sanPhamDto->Ma = $row["Ma"];
					$sanPhamDto->TenSanPham = $row["TenSanPham"];
					$sanPhamDto->SoLuong = $row["SoLuong"];
					$sanPhamDto->DonGiaGoc = $row["DonGiaGoc"];
					$sanPhamDto->DonGiaBan = $row["DonGiaBan"];
					$sanPhamDto->DacDiemSP = $row["DacDiemSP"];
					$sanPhamDto->NgayDang = $row["NgayDang"];
					$sanPhamDto->NgayCapNhat = $row["NgayCapNhat"];
					$sanPhamDto->NguoiCapNhat = $row["NguoiCapNhat"];
					$sanPhamDto->DaXoa = $row["DaXoa"];
					$sanPhamDto->NgayXoa = $row["NgayXoa"];
					$sanPhamDto->NguoiXoa = $row["NguoiXoa"];
					$sanPhamDto->MaLoaiSP = $row["MaLoaiSP"];
					$sanPhamDto->HinhAnh =$row["HinhAnh"];
					$sanPhamDto->LuotXem = $row["LuotXem"];					
					
					array_push($lstKetQua, $sanPhamDto);		
				}
			}
			
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstKetQua = null;
		}
		return $lstKetQua;
	}
	

}

?>